//
//  ZWDownloadViewController.m
//  wan
//
//  Created by apple on 16/2/28.
//  Copyright © 2016年 MyCompany. All rights reserved.
//

#import "ZWDownloadViewController.h"
#import "UIBarButtonItem+CH.h"
#import "ZWDownloadPlayHeaderView.h"
#import "ZWHeader.h"

@interface ZWDownloadViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) ZWDownloadPlayHeaderView *downloadPlayHeaderView;
@property (nonatomic, strong) UITableView *scenicView;
@property (nonatomic, strong) UITableView *featureView;
@property (nonatomic, strong) UIButton *scenicButton;
@property (nonatomic, strong) UIButton *featureButton;
@end

@implementation ZWDownloadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    //设置导航栏
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"返回" target:self action:@selector(returnClick)];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"顶部"] forBarMetrics:UIBarMetricsDefault];
    self.navigationItem.title = @"我的收藏";
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor colorWithWhite:0.1 alpha:1.0], NSFontAttributeName:[UIFont boldSystemFontOfSize:15]};
    
    //设置按钮
    self.scenicButton = [[UIButton alloc] initWithFrame:CGRectMake(60, 64, 80, 40)];
    [self.scenicButton setTitle:@"已下载" forState:UIControlStateNormal];
    [self.scenicButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.scenicButton setTitleColor:[UIColor colorWithRed:255.0 / 255 green:88.0 / 255 blue:0 / 255 alpha:1.0] forState:UIControlStateSelected];
    self.scenicButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [self.scenicButton addTarget:self action:@selector(scenicClick:) forControlEvents:UIControlEventTouchUpInside];
    self.scenicButton.selected = YES;
    [self.view addSubview:self.scenicButton];
    
    self.featureButton = [[UIButton alloc] initWithFrame:CGRectMake(WIDTH - 140, 64, 80, 40)];
    [self.featureButton setTitle:@"正在下载" forState:UIControlStateNormal];
    [self.featureButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.featureButton setTitleColor:[UIColor colorWithRed:255.0 / 255 green:88.0 / 255 blue:0 / 255 alpha:1.0] forState:UIControlStateSelected];
    self.featureButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [self.featureButton addTarget:self action:@selector(featureClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.featureButton];
    
    UIImageView *sep = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"分割线"]];
    sep.frame = CGRectMake(WIDTH / 2, 75, 1, 23);
    [self.view addSubview:sep];
    
    //设置顶部xib
    self.downloadPlayHeaderView = (ZWDownloadPlayHeaderView *)[[[NSBundle mainBundle] loadNibNamed:@"ZWDownloadPlayHeaderView" owner:self options:nil] lastObject];
    self.downloadPlayHeaderView.frame = CGRectMake(0, 104, WIDTH, 44);
    [self.view addSubview:self.downloadPlayHeaderView];
    
    //设置tableview
    self.scenicView = [[UITableView alloc] initWithFrame:CGRectMake(0, 148, WIDTH, 600) style:UITableViewStylePlain];
    self.scenicView.dataSource = self;
    self.scenicView.delegate = self;
    self.scenicView.rowHeight = 60;
    [self.view addSubview:self.scenicView];
    
    self.featureView = [[UITableView alloc] initWithFrame:CGRectMake(0, 148, WIDTH, 600) style:UITableViewStylePlain];
    self.featureView.hidden = YES;
    self.featureView.dataSource = self;
    self.featureView.delegate = self;
    self.featureView.rowHeight = 60;
    [self.view addSubview:self.featureView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playClick:) name:@"play" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deleteClick:) name:@"delete" object:nil];
}

- (void)returnClick
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)playClick:(UIButton *)button
{
    NSLog(@"播放全部");
}

- (void)deleteClick:(UIButton *)button
{
    NSLog(@"批量处理");
}

- (void)scenicClick:(UIButton *)button
{
    self.scenicView.hidden = NO;;
    self.featureView.hidden = YES;
    
    self.scenicButton.selected = YES;
    self.featureButton.selected = NO;
}

- (void)featureClick:(UIButton *)button
{
    
    self.scenicView.hidden = YES;
    self.featureView.hidden = NO;
    
    self.scenicButton.selected = NO;
    self.featureButton.selected = YES;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.scenicView) {
        return 20;
    } else {
        return 10;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.scenicView) {
        static NSString *scenicID = @"scenic";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:scenicID];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:scenicID];
        }
        return cell;
    } else {
        static NSString *featureID = @"feature";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:featureID];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:featureID];
        }
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.backgroundColor = [UIColor colorWithRed:248.0 / 255 green:248.0 / 255 blue:248.0 / 255 alpha:1.0];
}

@end
